import wybTable from '@/components/wyb-table/wyb-table.vue'
import member from '@/components/selectMember/selectMember.vue'
import {getUrlParam,filterData2} from '@/static/js/filter.js'
export default {
	components: {
		wybTable,
		member
	},
	data() {
		const currentDate = this.getDate({
			format: true
		})
		return {
			data:{},
			organ:'',
			examinationDate:'',
			userName:'',
			entrance:'',
			start:'',
			start_date:'',
			end:'',
			examinationOrgan:'',
			enterData:[{inputName:'身高',inputCompany:'m',inputData:'',name:'height'},{inputName:'体重',inputCompany:'kg',inputData:'',name:'weight'},{inputName:'体质指数',inputCompany:'kg/m²',inputData:'',name:'bodyIndex'},{inputName:'心率',inputCompany:'次/分',inputData:'',name:'heartRate'},{inputName:'血压',inputCompany:'mmHg',inputData:'',name:'bloodPressure'},{inputName:'腰围',inputCompany:'cm',inputData:'',name:'waist'},{inputName:'空腹血糖',inputCompany:'mmol/L',inputData:'',name:'bloodGlucose'},{inputName:'白细胞计数',inputCompany:'10/L',inputData:'',name:'whiteCell'},{inputName:'红细胞计数',inputCompany:'10¹²/L',inputData:'',name:'redCell'},{inputName:'血红蛋白',inputCompany:'g/L',inputData:'',name:'hemoglobin'},{inputName:'血小板计数',inputCompany:'109/L',inputData:'',name:'plateletCount'},{inputName:'总胆固醇',inputCompany:'mmol/L',inputData:'',name:'cholesterol'},{inputName:'三酰甘油',inputCompany:'mmol/L',inputData:'',name:'triacylglycerol'},{inputName:'低密度脂蛋白胆固醇',inputCompany:'mmol/L',inputData:'',name:'lowCholesterol'},{inputName:'高密度脂蛋白胆固醇',inputCompany:'mmol/L',inputData:'',name:'highCholesterol'},{inputName:'谷丙转氨酶',inputCompany:'U/L',inputData:'',name:'aminotransferase'},{inputName:'总胆红素',inputCompany:'umol/L',inputData:'',name:'bilirubin'},{inputName:'血尿素氮',inputCompany:'mmol/L',inputData:'',name:'bloodUreaNitrogen'},{inputName:'血肌酐',inputCompany:'umol/L',inputData:'',name:'bloodMuscle'},{inputName:'血尿酸',inputCompany:'umol/L',inputData:'',name:'bloodUricAcid'},{inputName:'餐后两小时血糖',inputCompany:'umol/L',inputData:'',name:'towBloodGlucose'}],
			auxiliaryData:[{name:'心电图',value:''},{name:'腹部超声',value:''},{name:'X线胸片',value:''}],
			dynamicData:[], //辅助项目添加其他
			memberdialog:false,
			dialog:false,
			showModal:false,
			showContent:'',
			status: '' ,//存档状态
			isCheckDxData:[], //选中的单项数据
			dataArr:['height','weight','bodyIndex','heartRate','bloodPressure','waist','bloodGlucose','whiteCell','redCell','hemoglobin','plateletCount','cholesterol','triacylglycerol','lowCholesterol','highCholesterol','aminotransferase','bilirubin','bloodUreaNitrogen','bloodMuscle','bloodUricAcid','towBloodGlucose'],
			// 所有体检项目分页参数
			tiOption:{pageIndex:1,totalCount:0,},
			abnormalName:'',
			// 异常体检表头
			dialogHeaders2:[{label: '科室',key:'sectionName'},{label: '名称',key:'name'},{label: '参考范围',key:'va'},{label: '单位',key:'units'}],
			dialogcontents: [],
			isCheckYcData:[], //选中的异常数据
			parameterValues:[],
			// 异常体检表头
			dialogHeaders3:[{label: '名称',key:'name',width: 200,},{label: '结果',key:'input',width: 150,},{label: '参考范围',key:'va',width: 200,},{label: '单位',key:'units',width: 200,}],
			id:'',
			dataId:[],
			bloodPressure1:'',
			bloodPressure2:'',
		}
	},
	mounted() {
		this.id = getUrlParam('id');
		if(this.id) {
			this.$api.HealthExaminationReportDetails(this.id).then((res) => {
				if(res.data.returnCode == 200) {
					this.userId = res.data.result && res.data.result.newExaminationReportRes.userId;
					this.userName = res.data.result && res.data.result.newExaminationReportRes.userName;
					this.organ = res.data.result && res.data.result.newExaminationReportRes.organ;
					this.examinationDate = res.data.result && res.data.result.newExaminationReportRes.examinationDate;
					this.data = res.data.result.newExaminationReportRes;
					this.bloodPressure1 = res.data.result.newExaminationReportRes.bloodPressure == '-1'?'':res.data.result.newExaminationReportRes.bloodPressure.split('/')[0]
					this.bloodPressure2 = res.data.result.newExaminationReportRes.bloodPressure == '-1'?'':res.data.result.newExaminationReportRes.bloodPressure.split('/')[1]
					this.auxiliaryData = res.data.result.newExaminationReportRes.auxiliaryExamination && JSON.parse(res.data.result.newExaminationReportRes.auxiliaryExamination).map((a)=>{
						if(a.value == -1){
							a.value = ''
						}
						return a
					});
					this.isCheckDxData = res.data.result && res.data.result.newExaminationReportRes.individuals.length > 0 && res.data.result.newExaminationReportRes.individuals.map((a)=>{ a.itemValue = a.value;a.itemName = a.name;a.itemId = a.key; return a}) || [];
					this.enterData.map((a,ai)=>{
						if(res.data.result.newExaminationReportRes[a.name]){
							if(res.data.result.newExaminationReportRes[a.name] == -1){
								res.data.result.newExaminationReportRes[a.name] = ''
							}
							a.dataId = ai+''
						}
						return a
					})
				}
			})
		}
		
	},
	methods: {
		// 日期
		bindDateChange(e) {  //日期选择
			this.examinationDate = e.target.value;
		},
		// 获取当前时间
		getDate(type) {
		   const date = new Date();
		   let year = date.getFullYear();
		   let month = date.getMonth() + 1;
		   let day = date.getDate();
		  
		   if (type === 'start') {
			year = year - 60;
		   } else if (type === 'end') {
			year = year + 2;
		   }
		   month = month > 9 ? month : '0' + month;;
		   day = day > 9 ? day : '0' + day;
		   return `${year}-${month}-${day}`;
		},
		// 添加辅助项目
		addDynamicData() {
			this.parameterValues.push({name:'',value:''})
		},
		del(e) {
			this.parameterValues.splice(e,1)
		},
		// 选中的异常
		onCheck(e){
			let data = e.data.map((res)=>{return res.lineData})
			if(this.type == 'yczb'){
				this.isCheckYc = data
			}else{
				this.isCheckDx = data
			}
		},
		// 单项数据回调
		dxgetcallbackdata(e){
			let keyArr = Object.keys(e[0]).map((a)=>{
				return a
			})
			const reportItemS = e[1].map((a,ai)=>{
				keyArr.map((b,bi)=>{
					if(a.itemId == b){
						a.itemId = b;
						a.itemValue = e[0][b]
					}else if(a.reportNo == b){
						a.itemId = b;
						a.itemValue = e[0][b]
					}
				})
				return a
			})
			this.dxgetcallback = reportItemS
		},
		dialogSubmit(){
			const isrepeat = []
			this.enterData && this.enterData.map((a)=>{
				this.isCheckDx.map((c)=>{
					if(c.itemName == a.inputName){
						isrepeat.push(c)
					}
				})
			})
			if(isrepeat.length > 0){
				uni.showToast({title:isrepeat[0].itemName+'重复',icon:'none'})
				return;
			}
			if(this.isCheckDxData.length <= 0){
				this.isCheckDxData = this.isCheckDx
			}else{
				const hasRepeat = this.isCheckDxData.filter((a,ai)=>{
					const arr = this.isCheckDx.filter((b,bi)=>{return b.name == a.name})
					return arr.length > 0
				})
				if(hasRepeat.length > 0){
					uni.showToast({title:'已有重复项',icon:'none'})
					return;
				}
				this.isCheckDxData = this.isCheckDxData.concat(this.isCheckDx)
			}
			this.dialog = false;
		},
		onSearch(){
			this.pageIndex = 1
			this.getReportAbnormalPageAxios({abnormalName:this.abnormalName,pageIndex:this.pageIndex,pageSize:20})
		},
		// 获取所有异常
		getReportAbnormalPageAxios(data){
			uni.showLoading({title:'加载中...'})
			this.$api.getReportAbnormalPage(data).then((res)=>{
				uni.hideLoading()
				this.totalCount = Math.ceil(res.data.result.totalCount / 20)
				this.dialogcontents = res.data.result.list.map((e)=>{
					return {
						name:e.abnormalName,
						Wpercentage: '82vw',
						reportNo:e.abnormalCode
					}
				})
			})
		},
		// 获取所有体检项目
		getReportTemplateStencilPageAxios(data){
			uni.showLoading({title:'加载中...'})
			this.$api.getReportTemplateStencilPage(data).then((res)=>{
				uni.hideLoading()
				this.tiOption.totalCount = Math.ceil(res.data.result.totalCount / 20)
				this.dialogcontents = res.data.result.list.map((e)=>{
					e.name = e.itemName
					e.maxValue = e.maxVa
					e.minValue = e.minVa
					e.va = e.minVa+'~'+e.maxVa
					return e
				}).filter(b=>{
					if(this.sex == '男'){
						return b.itemName.indexOf('女') <= -1
					}else{
						return b.itemName.indexOf('男') <= -1
					}
				})
			})
		},
		hidememberdialog(){
			this.memberdialog = false
		},
		// 选择会员
		chooseUser(e){
			this.sex = e.sex == 2 ? '女':'男',
			this.userId = e.userId;
			this.userName = e.userName;
		},
		//计算体质指数
		basicInput(e){
			if(e.currentTarget.dataset.id == 0 || e.currentTarget.dataset.id == 1){
				if(this.data.weight && this.data.height){
					this.data.bodyIndex = (this.data.weight / Math.pow((this.data.height /100),2)).toFixed(1)
					this.enterData[2].dataId = e.currentTarget.dataset.id+''
				}else{
					this.data.bodyIndex =''
					this.enterData[2].dataId = ''
				}
			}
			// if(e.detail.value == ''){
			// 	this.enterData[e.currentTarget.dataset.id].dataId = ''
			// }else{
			// 	this.enterData[e.currentTarget.dataset.id].dataId = e.currentTarget.dataset.id+''
			// }
		},
		seveAdd(e){		
			if(!this.userId) {
				uni.showToast({
					title:'请选择会员',
					icon:'none'
				})
				return
			}
			if(!this.organ) {
				uni.showToast({
					title:'请填写体检机构',
					icon:'none'
				})
				return
			}
			
			if(!this.examinationDate) {
				uni.showToast({
					title:'请选择体检时间',
					icon:'none'
				})
				return
			}
			const arr = this.auxiliaryData.concat(this.parameterValues)
			if(arr[0].value == '') {arr[0].value = -1.0}
			if(arr[1].value == '') {arr[1].value = -1.0}
			if(arr[2].value == '') {arr[2].value = -1.0}
			const individuals = this.isCheckDxData && this.isCheckDxData.length > 0 && this.isCheckDxData.map((a)=>{
				return {name:a.itemName,value:a.itemValue,key:a.itemId}
			}) || []
			let data = {}
			this.enterData.map((a)=>{
				if(!this.data[a.name]){
					data[a.name] =  -1.0
				}else{
					data[a.name] = Number(this.data[a.name])
				}
			})
			data.bloodPressure = !this.bloodPressure1 && !this.bloodPressure2 ? '-1' : (this.bloodPressure1 || -1) + '/' + (this.bloodPressure2 || -1 )
			data.parameterValues = arr
			data.individuals = individuals
			data.userId = this.userId
			data.organ = this.organ
			data.examinationDate = this.examinationDate
			data.status = e
			data.healthId = JSON.parse(localStorage.userInfo).id
			this.upData = data
			if(this.id) {
				data.id = this.id
				if(e == 1) {
					this.$api.HealthExaminationReportUpdate(data).then((res)=>{
						if(res.data.returnCode == 200) {
							uni.showToast({
								title:'保存成功',
								icon:'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url:'/pages/quickly/medicalReport/list1'
								})
							},1000)
						}
					})
				}else {
					this.showModal = true;
					this.showContent = '确认信息无误，签名并发送给用户存入健康档案，发送后该信息将不可修改。';
				}
				
			}else {
				if(e == 1) {
					this.$api.HealthExaminationReportSave(data).then((res)=>{
						if(res.data.returnCode == 200) {
							uni.showToast({
								title:'保存成功',
								icon:'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url:'/pages/quickly/medicalReport/list1'
								})
							},1000)
						}
					})
				}else {
					this.showModal = true;
					this.showContent = '确认信息无误，签名并发送给用户存入健康档案，发送后该信息将不可修改。'
				}
				
			}
			
		},
		Modalconfirm(){
			this.upData.status = 2;
			this.upData.id = this.id
			if(!this.id) {
				delete this.upData.id
			}
			(this.id ? this.$api.HealthExaminationReportUpdate(this.upData) : this.$api.HealthExaminationReportSave(this.upData)).then((res)=>{
				if(res.data.returnCode == 200) {
					uni.showToast({
						title:'签发成功',
						icon:'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url:'/pages/quickly/medicalReport/list1'
						})
					},1000)
				}
			})
		},

	}
}

